﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace eDoc.Controls.FTP
{
    public partial class ucAddFTP : System.Web.UI.UserControl
    {
        private int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"].ToString() != "")
            {
                id = Convert.ToInt32(Request.QueryString["id"]);
            }

            if (!IsPostBack)
            {
                txtPartnerCode.Text = BO.Common.PartnerCode;
                BindData();
            }
        }

        private void BindData()
        {
            if (id == 0)
            {
                ltrFtpTitle.Text = "Add";
            }
            else
            {
                DataTable dt = new DataTable();
                dt = BO.Data.Select_FTP(id);
                if (dt != null && dt.Rows.Count > 0)
                {
                    txtName.Text = dt.Rows[0]["Name"].ToString();
                    txtHost.Text = dt.Rows[0]["HostFTP"].ToString();
                    txtUserFTP.Text = dt.Rows[0]["UserFTP"].ToString();
                    txtPort.Text = dt.Rows[0]["PortFTP"].ToString();
                    txtPassFTP.Text = dt.Rows[0]["PassFTP"].ToString();
                    txtPartnerCode.Text = dt.Rows[0]["PartnerCode"].ToString();
                    chkPassive.Checked = Convert.ToBoolean(dt.Rows[0]["passive"]);
                    chkTSL.Checked = Convert.ToBoolean(dt.Rows[0]["TSL"]);
                    chkActive.Checked = Convert.ToBoolean(dt.Rows[0]["Active"]);
                }
                ltrFtpTitle.Text = "Edit FTP";
                btAddFTP.Text = "Save";
            }
        }

        protected void btAddFTP_Click(object sender, EventArgs e)
        {
            if (txtHost.Text.Trim() == "")
            {
                lblNote.Text = "Chưa nhập Host FTP!";
                return;
            }
            if (txtUserFTP.Text.Trim() == "")
            {
                lblNote.Text = "Chưa nhập User FTP!";
                return;
            }
            if (txtPassFTP.Text.Trim() == "")
            {
                lblNote.Text = "Chưa nhập Pass FTP!";
                return;
            }
            if (txtPort.Text.Trim() == "")
            {
                lblNote.Text = "Chưa nhập Port FTP!";
                return;
            }

            if (id == 0)
            {
                BO.Data.Insert_FTP(txtPartnerCode.Text.Trim(), txtHost.Text.Trim(), txtPort.Text.Trim(), txtUserFTP.Text.Trim(), txtPassFTP.Text.Trim(), 
                    Convert.ToBoolean(chkPassive.Checked), txtName.Text.Trim(), Convert.ToInt32(ddlType.SelectedValue),
                    Convert.ToBoolean(chkTSL.Checked), Convert.ToBoolean(chkActive.Checked));
            }
            else
            {
                BO.Data.Update_FTP(id, txtPartnerCode.Text.Trim(), txtHost.Text.Trim(), txtPort.Text.Trim(), txtUserFTP.Text.Trim(), txtPassFTP.Text.Trim(),
                    Convert.ToBoolean(chkPassive.Checked), txtName.Text.Trim(), Convert.ToInt32(ddlType.SelectedValue),
                    Convert.ToBoolean(chkTSL.Checked), Convert.ToBoolean(chkActive.Checked));
            }
            Response.Redirect("/listftp.aspx");
        }
    }
}